package me.leiusual.sharing.ui.order.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_order_list_parent.*
import me.leiusual.sharing.api.model.orderdetail.Model
import me.leiusual.sharing.api.model.orderlist.Content
import me.leiusual.sharing.util.extension.spToPx
import me.leiusual.sharing.util.extension.toSpanned

/**
 * author: leiusual
 * create on: 2019-07-31
 * description:
 */
class OrderListParentViewHolder(
    override val containerView: View,
    private val clickListener: (Content) -> Unit
) : RecyclerView.ViewHolder(containerView), LayoutContainer, View.OnClickListener {

  private lateinit var content: Content

  init {
    containerView.setOnClickListener(this)
    recycler_view.adapter = OrderListChildAdapter()
  }

  fun bind(content: Content) {
    this.content = content

    text_view_order_code.text = "订单号：${content.code}"

    // 价格
    val size = 10f.spToPx(containerView.context)
    val builder = StringBuilder()
    builder.append("""<font color="red">""")
    builder.append("""<size_$size>￥</size_$size>""")
    builder.append("""${content.amount}""")
    builder.append("""</font>""")
    text_view_price.text = builder.toString().toSpanned()

    text_view_status.text = content.orderStatusStr
    if (content.deliveryStatus in arrayOf(Model.DELIVERY_STATUS_CONFIRMING, Model.DELIVERY_STATUS_CONFIRMED)) {
      text_view_status.text = content.deliveryStatusStr
    }

    val adapter = recycler_view.adapter as OrderListChildAdapter
    adapter.submitList(content.orderDetails)
  }

  override fun onClick(v: View?) {
    clickListener(content)
  }
}